{extend name="public/container"}
{block name="content"}
<style type="text/css">
    .avatar-uploader .el-upload {
        width: 350px;
        height: 135px;
    }
    .target-link .el-select {width: 100%;}
</style>
<div id="myApp">
     <div class="douxue-filter padding-20 flex flex-between" style="margin-bottom:20px;">
        <el-form :inline="true" :model="where" size="mini" class="flex">
            <div class="flex flex-between">
              <el-form-item>
                <el-select
                    size="small"
                    v-model="where.status"
                    placeholder="是否启用"
                    :clearable=true>
                    <el-option label="是" :value="1"></el-option>
                    <el-option label="否" :value="0"></el-option>
                </el-select>
              </el-form-item>

              <el-form-item>
                <el-input size="small" v-model="where.title" placeholder="请输入轮播标题">
                     <el-button slot="append" size="small" type="primary" @click="doLoadRows(1)" icon="el-icon-search"></el-button>
                </el-input>
              </el-form-item>
            </div>
        </el-form>
        <el-button type="primary" size="mini" @click="doAdd()">新增轮播</el-button>
    </div>

    <div class="padding-10">
        <el-table
            :data="rows"
            style="width: 100%"
            size="mini">
            <el-table-column
                prop="id"
                label="ID"
                width="80"
                align="left">
            </el-table-column>
            <el-table-column
                prop="title"
                label="轮播标题"
                width="200"
                align="left">
            </el-table-column>
            <el-table-column
                prop="cover"
                label="轮播图片"
                align="left">
                <template slot-scope="scope">
                    <el-image
                        style="width: 100px; height: 100px"
                        :src="scope.row.cover"
                        :preview-src-list="[scope.row.cover]"
                        fit="contain">
                    </el-image>
                </template>
            </el-table-column>

            <el-table-column
                prop="link"
                label="跳转地址"
                width="300"
                align="left">
                <template slot-scope="scope">
                    <div v-if="scope.row.rel_id>0">
                        <div>类型：{{ scope.row.rel_type_display}}</div>
                        <div style="white-space: nowrap; text-overflow: ellipsis; overflow: hidden;">地址：{{ scope.row.rel_id_display}}</div>
                    </div>
                    <el-link :underline="false" type="danger" v-else>未设置</el-link>
                </template>
            </el-table-column>

            <el-table-column
                prop="banner_sort"
                label="排序"
                width="120"
                cell-click="cell"
                align="left">
            </el-table-column>
            <el-table-column
                prop="status"
                label="是否显示"
                width="120"
                align="left">
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.status"
                        active-color="#ff6a6c"
                        inactive-color="#999"
                        :active-value="1"
                        :inactive-value="0"
                        @change="doSetStatus(scope.row)">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column
                label=""
                width="90"
                align="left">
                <template slot-scope="scope">
                    <el-link type="primary" size="mini" :underline="false" @click="doAdd(scope.row)">编辑</el-link>
                    <el-link type="danger" size="mini" :underline="false" @click="doDelete(scope.row.id)">删除</el-link>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination">
            <el-pagination
                small
                :hide-on-single-page="true"
                layout="prev, pager, next"
                :total="rowsCount"
                :page-size="where.limit"
                @current-change="doLoadRows">
            </el-pagination>
        </div>
    </div>
    <!-- 添加/编辑   -->
    <el-dialog title="添加/编辑" v-if="dialogFormVisible" :visible.sync="dialogFormVisible" @close='closeDialog'>
        <el-form :model="formData" :rules="rules" ref="formData" label-width="80px" >
            <el-form-item label="轮播标题" prop="title">
                <el-input v-model="formData.title" placeholder="轮播标题" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="轮播图" prop="cover">
                <el-upload
                        class="avatar-uploader"
                        action="/admin/upload/image"
                        accept="image/jpeg,image/png,image/jpg"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload">
                    <img v-if="formData.cover" :src="formData.cover" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    <div slot="tip" class="el-upload__tip">尺寸建议为750×270或等比例，格式jpg/png且不超过1MB</div>
                </el-upload>
            </el-form-item>
			
            <el-form-item label="跳转地址">
                <div class="flex">
                    <div class="target-type" style="width:100px;margin-right: 10px;">
                        <el-select
                            size="small"
                            v-model="formData.rel_type"
                            placeholder="跳转类型"
                            @change="switchRelType"
                            :clearable=true>
                            <el-option label="线路" :value="1"></el-option>
                            <el-option label="商城" :value="2"></el-option>
                            <el-option label="图标推荐位" :value="3"></el-option>
                            <el-option label="文字推荐位" :value="4"></el-option>
                            <el-option label="自定义PATH" :value="5"></el-option>
                        </el-select>
                    </div>
                    <div class="target-link" style="flex:1">
                        <template v-if="formData.rel_type==5">
                            <el-input type="text" v-model="formData.mp_path" size="mini" placeholder="输入自定义路径，如/pages/index/index"></el-input>
                        </template>
                        <template v-else>
                            <el-select
                                v-model="formData.rel_id"
                                size="small"
                                filterable
                                placeholder="请选择资源名称">
                                <el-option
                                  v-for="item in relData"
                                  :key="item.id"
                                  :label="item.title"
                                  :value="item.id">
                                </el-option>
                            </el-select>
                        </template>
                    </div>
                </div>
            </el-form-item>
			
            <el-form-item label="排序">
                <el-input type="number" v-model="formData.banner_sort" placeholder="排序" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="是否启用">
                <el-select size="small" placeholder="是否启用" v-model="formData.status">
                    <el-option label="否" value="0"></el-option>
                    <el-option label="是" value="1"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit('formData')">确 定</el-button>
                <el-button @click="dialogFormVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</div>
{/block}
{block name="script"}
<script>
    var _formData = {
        id:0,
        title: '',
        rel_type: 1,
        banner_sort: '',
        status: '1',
        cover: '',
        mp_path: '',
    }
    var my = new Vue({
        el: "#myApp",
        data() {
            return{
                dialogFormVisible: false,
                formData:_formData,
                relUrlObj: {},
                relData: [],
                reldataloading: false,
                rules:{
                    title: [
                        { required: true, message: '轮播标题不能为空', trigger: 'blur' },
                    ],
                    cover: [
                        { required: true, message: '轮播图不能为空', trigger: 'change' },
                    ]
                },
                rowsCount: 0,
                rows: [],
                where:{
                    page:1,
                    limit: pageSize,
                    status:'',
                    title:'',
                }
            }
        },
        methods: {
            handleAvatarSuccess(res, file) {
                 if ( res.code == 200 ) {
                    return this.formData.cover = res.url;
                }
                return this.$message.error(res.state);
            }
            ,beforeAvatarUpload(file) {
                const isLt2M = file.size / 1024 / 1024 < 5;
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 5MB!');
                }
                return isLt2M;
            }
            ,doLoadRows (pn) {
                this.where.page = pn || 1
                axios.get(Url({a:'get_list' , q:this.where})).then(res => {
                    this.rows = res.data.data
                    this.rowsCount = res.data.count
                })
            }
            // 设置状态
            ,doSetStatus (row) {
                axios.get(Url({a:'set_status',q:{status:row.status,id:row.id}})).then(res => {
                    this.$message({
                        message: res.data.msg,
                        type: res.data.code === 200 ? 'success' : 'error',
                        onClose: _ => {
                            if (res.data.code !== 0) {
                                this.doLoadRows()
                            }
                        }
                    })
                })
            }
            // 编辑
            ,doAdd ( item ) {
                if ( item != undefined ) {
                    this.formData = item;
                    if ( this.formData.rel_type > 0 ) {
                        this.switchRelType(this.formData.rel_type)
                    }
                } else {
                    this.formData = _formData
                }
                this.dialogFormVisible = true
            }
            ,onSubmit (formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post( Url({a:'save'}) , this.formData)
                            .then(res => {
                                this.$message({
                                    message:'操作成功',
                                    type:'success',
                                    duration:1500
                                });
                                this.dialogFormVisible = false;
                                this.doLoadRows(1)
                            }).catch(_ => {
                            this.$message({
                                message: '操作失败',
                                type: 'error'
                            })
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
            ,doDelete (id) {
                this.$confirm('确认删除该条记录?').then(_ => {
                    axios.get(Url({a:'delete' , q:{id:id}})).then(res => {
                        this.$message({
                            message: res.data.msg,
                            type: res.data.code === 200 ? 'success' : 'error',
                        })
                    }).catch(_ => {
                        this.$message({
                            message: '删除失败',
                            type: 'error'
                        })
                    })

                    return this.doLoadRows();
                }).catch(_ => {})
            }
            // 关闭
            ,closeDialog () {
                this.$refs['formData'].clearValidate();
            }
            ,loadRelData() {
                let _url = Url(this.relUrlObj)
                axios.get(_url).then(res => {
                    this.relData = res.data.data
                })
            }
            ,switchRelType(relType) {
                if ( relType != this.formData.rel_type ) {
                    this.formData.rel_id = 0
                }

                if ( relType == 1 ) {
                    this.relUrlObj = {
                        c: 'routes.routes',
                        a: 'get_list',
                        q: {limit: 1000}
                    }
                }

                if ( relType == 2 ) {
                    this.relUrlObj = {
                        c: 'golds.goods',
                        a: 'get_list',
                        q: {limit: 1000}
                    }
                }

                if ( relType == 3 ) {
                    this.relUrlObj = {
                        c: 'maintain.icon_recomand',
                        a: 'get_list',
                        q: {limit: 1000,type: 1}
                    }
                }

                if ( relType == 4 ) {
                    this.relUrlObj = {
                        c: 'maintain.icon_recomand',
                        a: 'get_list',
                        q: {limit: 1000,type:2}
                    }
                }

                let _url = Url(this.relUrlObj)
                axios.get(_url).then(res => {
                    this.relData = res.data.data
                })
            }
        },
        mounted(){
            this.doLoadRows()
        }
    })
</script>
{/block}